package com.masterclient.app.widget;

import android.content.Context;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.eoner.baselibrary.utils.ViewTransformUtil;
import com.masterclient.app.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

/**
 * <pre>
 *     Copyright    : Copyright (c) 2020.
 *     Authour      : JiaoYa.
 *     Created Time : 2020/4/24.
 *     Desc         :
 *     Alter By     :
 * </pre>
 *
 * @author JiaoYa
 */

public class MyDealerRefreshHead extends LinearLayout implements RefreshHeader{

    private Context mContext;
    private ImageView imageView;
    private ImageView ivText;
    private SmartRefreshLayout refreshLayout;
    private Animation mAnimation;

    public MyDealerRefreshHead(Context context) {
        super(context);
        initView(context);
    }

    public MyDealerRefreshHead(Context context, SmartRefreshLayout refreshLayout) {
        super(context);
        this.mContext = context;
        this.refreshLayout = refreshLayout;
        initView(context);
    }

    public MyDealerRefreshHead(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.initView(context);
    }

    public MyDealerRefreshHead(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initView(context);
    }

    public void setImageView(String imgUrl) {
        ivText.setVisibility(VISIBLE);
        ViewTransformUtil.glideImageView(mContext, imgUrl, ivText, R.mipmap.defaults);
    }

    public void destroyAnimation(){
        if (mAnimation != null) {
            mAnimation.cancel();
            mAnimation = null;
        }
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.loading_dealer_lottie, this);
        imageView = view.findViewById(R.id.lottie);
        ivText = view.findViewById(R.id.iv_text);
        ivText.setVisibility(VISIBLE);
        mAnimation = AnimationUtils.loadAnimation(mContext, R.anim.rotate_repeat);
        LinearInterpolator lin = new LinearInterpolator();
        mAnimation.setInterpolator(lin);
        imageView.startAnimation(mAnimation);
    }

    @Override
    @NonNull
    public View getView() {
        ///真实的视图就是自己，不能返回null
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        //指定为平移，不能null
        return SpinnerStyle.Translate;
    }

    @Override
    public void onStartAnimator(RefreshLayout layout, int headHeight, int maxDragHeight) {
    }

    @Override
    public int onFinish(RefreshLayout layout, boolean success) {
        //延迟500毫秒之后再弹回
        return 0;
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onInitialized(RefreshKernel kernel, int height, int maxDragHeight) {
    }

    @Override
    public void onMoving(boolean isDragging, float percent, int offset, int height, int maxDragHeight) {

    }

    @Override
    public void onReleased(@NonNull RefreshLayout refreshLayout, int height, int maxDragHeight) {

    }

    @Override
    public void onHorizontalDrag(float percentX, int offsetX, int offsetMax) {
    }

    @Override
    public void setPrimaryColors(@ColorInt int... colors) {
    }

    @Override
    public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
    }
}
